from rest_framework import serializers

from book.models import BookInfo, PeopleInfo


class PeopleRelatedSerializer(serializers.Serializer):
    id = serializers.IntegerField()
    name = serializers.CharField()
    password = serializers.CharField()


class BookInfoSerializer(serializers.Serializer):
    # read_only 只用于序列化使用,反序列化忽略
    # write_only 只用于反序列化使用,序列化忽略
    id = serializers.IntegerField(read_only=True)
    name = serializers.CharField(max_length=10, min_length=5)
    pub_date = serializers.DateField()
    readcount = serializers.IntegerField()
    commentcount = serializers.IntegerField(max_value=10000, min_value=1)

    # people = PeopleRelatedSerializer(many=True)

    def validate_readcount(self, value):
        # 单个字段验证
        if value < 0:
            # raise Exception('阅读量不能小于0')
            raise serializers.ValidationError('阅读量不能小于0')
        return value

    def validate(self, attrs):
        # 多字段验证
        readcount = attrs.get('readcount')
        commentcount = attrs.get('commentcount')
        if readcount < commentcount:
            raise serializers.ValidationError('阅读量不能小于评论量')
        return attrs

    # 保存数据
    def create(self, validated_data):
        return BookInfo.objects.create(**validated_data)

    # 更新数据
    def update(self, instance, validated_data):
        instance.name = validated_data.get('name', instance.name)
        instance.pub_date = validated_data.get('pub_date', instance.pub_date)
        instance.readcount = validated_data.get('readcount', instance.readcount)
        instance.commentcount = validated_data.get('commentcount', instance.commentcount)
        instance.save()
        return instance


class PeopleInfoSerializer(serializers.Serializer):
    id = serializers.IntegerField()
    name = serializers.CharField()
    password = serializers.CharField()
    description = serializers.CharField()

    # 外键
    # book_id = serializers.IntegerField()

    # BookInfo book_id
    # book = serializers.PrimaryKeyRelatedField(queryset=BookInfo.objects.all())
    # book = serializers.PrimaryKeyRelatedField(read_only=True)

    # BookInfo __str__
    # book = serializers.StringRelatedField()

    # 所有数据
    book = BookInfoSerializer()
    
# 模型和类

class BookInfoModelSerializer(serializers.ModelSerializer):
    # 1.属性重写, 优先级高于 extra_kwargs
    # name = serializers.CharField(max_length=20, min_length=5)

    class Meta:
        model = BookInfo
        # 所有字段
        fields = '__all__'
        # 指定字段
        # fields = ['id', 'name']
        # 排除字段
        # exclude = ['id', 'name']
        # 2.添加扩展属性
        # extra_kwargs = {
        #     'name': {
        #         'max_length': 30,
        #         'min_length': 5
        #     }
        # }

        # 只读字段
        # read_only_fields = ['id']


class PeopleInfoModelSerializer(serializers.ModelSerializer):
    # book = serializers.PrimaryKeyRelatedField(queryset=BookInfo.objects.all())
    # name = serializers.CharField(max_length=20)
    # password = serializers.CharField(max_length=20)

    class Meta:
        model = PeopleInfo
        fields = ['id', 'book', 'name', 'password', 'is_delete']

        extra_kwargs = {
            'password': {
                'write_only': True
            },
            'is_delete': {
                'read_only': True
            }
        }
